//
//  LogInPassWordTableViewController.m
//  EasyToTesco2.0
//
//  Created by 袖手旁觀 on 16/6/17.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "LogInPassWordTableViewController.h"
#import "LogInPassWordTableViewCell.h"
#import "LogInPassWordFooterView.h"
#import "BindingPhoneTableViewCell.h"

@interface LogInPassWordTableViewController ()<LogInPassWordFooterDelegate,UITextFieldDelegate>

@property (nonatomic,strong)NSArray *logInPasswordArray;

@property (nonatomic,strong)NSArray *headerTextArray;

@property (nonatomic,strong)NSMutableArray *textFieldArray;

@end

static NSString *const reuseIdentifier = @"reuseIdentifier";

@implementation LogInPassWordTableViewController

- (NSArray *)logInPasswordArray {
    if (!_logInPasswordArray) {
        
        NSString *arrayPath = [[NSBundle mainBundle] pathForResource:@"LogInPassWordPlaceholder" ofType:@"plist"];
        
        _logInPasswordArray = [NSArray arrayWithContentsOfFile:arrayPath];
        
    }
    return _logInPasswordArray;
}

- (NSArray *)headerTextArray {
    if (!_headerTextArray) {
        NSString *arrayPath = [[NSBundle mainBundle] pathForResource:@"LogInPassWordText" ofType:@"plist"];
        _headerTextArray = [NSArray arrayWithContentsOfFile:arrayPath];
    }
    return _headerTextArray;
}

- (NSMutableArray *)textFieldArray {
    if (!_textFieldArray) {
        _textFieldArray = [NSMutableArray array];
    }
    return _textFieldArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"修改登录密码";
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = BACKGROUND_COLOR;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else if (section == 1) {
        return 2;
    } else {
        return 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BindingPhoneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    
    if (!cell) {
        cell = [[BindingPhoneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if (indexPath.section == 0) {
        cell.textField.hidden = YES;
        cell.rightLabel.hidden = NO;
        cell.rightLabel.text = USERNAME;
    }
    
    NSInteger place = indexPath.row + indexPath.section;
    
    if (indexPath.section == 2) {
        place = 3;
    }
    
    cell.leftImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"xiugai_%ld",place]];
    
    if (indexPath.section != 0) {
        cell.textField.placeholder = self.logInPasswordArray[place - 1];
        cell.textField.delegate = self;
        cell.textField.secureTextEntry = YES;
        cell.textField.keyboardType = UIKeyboardTypeDefault;
        cell.textField.tag = place - 1;
        [self.textFieldArray addObject:cell.textField];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 5;
    } else {
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 2) {
        return HEIGHT/5;
    } else {
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 2) {
        LogInPassWordFooterView *footer = [[LogInPassWordFooterView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/5)];
        footer.delegate = self;
        return footer;
    }
    return nil;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return nil;
    } else {
        return self.headerTextArray[section - 1];
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self clearTextFieldResignFirstResponder];
}

- (void)clearTextFieldResignFirstResponder {
    for (UITextField *textField in self.textFieldArray) {
        [textField resignFirstResponder];
    }
}

- (void)didViewClickClearTextField {
    [self clearTextFieldResignFirstResponder];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self clearTextFieldResignFirstResponder];
}

- (void)LogInPassWordFooterViewButton:(UIButton *)button {
    [self clearTextFieldResignFirstResponder];
    
    UITextField *oldTextField = self.textFieldArray[0];
    
    UITextField *newTextField = self.textFieldArray[1];
    
    UITextField *recurNewTextField = self.textFieldArray.lastObject;
    
    if (![recurNewTextField.text isEqualToString:newTextField.text]) {
        
        MBProgressHUD *hud = [MBProgressHUD showMessage:@"请确保两次密码输入无误" toView:self.view dimBackground:NO AndCompleteBlock:nil];
        
        [self.view addSubview:hud];
        return;
    }
    
    if (oldTextField.text.length == 0 || newTextField.text.length == 0 || recurNewTextField.text.length == 0) {
        MBProgressHUD *hud = [MBProgressHUD showMessage:@"请确保信息输入无误" toView:self.view dimBackground:NO AndCompleteBlock:nil];
        [self.view addSubview:hud];
        NSLog(@"请确保信息输入无误");
        return;
    }
    
    NSDictionary *params = @{
                             @"username":USERNAME,
                             @"newpaassword":recurNewTextField.text,
                             @"oldpaassword":oldTextField.text
                             };
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/Pub/editpassword" completeBlock:^(id object) {
        NSLog(@"%@",object);
        
        [JudgeBackDataCodeMessage JudgeBackDataCodeMessage:@"登录密码修改成功" andObjece:object andAddHUDView:self.view andPopViewController:^(id object) {
            NSLog(@"%@",object);
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.tag == 0) {
        [self.textFieldArray[1] becomeFirstResponder];
    }
    
    if (textField.tag == 1) {
        [self.textFieldArray[2] becomeFirstResponder];
    }
    
    if (textField.tag == 2) {
        [self LogInPassWordFooterViewButton:nil];
    }
    return YES;
}

@end
